package com.yzly.demo.aop;

import com.alibaba.fastjson.JSONObject;
import com.yzly.demo.utils.RequestGetJsonBody;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 用AOP记录日志
 */
@Component
@Aspect
public class TCPushLogAop {

    private Logger logger = LoggerFactory.getLogger(TCPushLogAop.class);

    @Before("execution(public * com.yzly.demo.controller.TCPushController.*(..))")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String methodName = joinPoint.getSignature().getName();//方法名
        Object[] args = joinPoint.getArgs();//请求参数数组
        String content = "";//body内容
        if (args != null && args.length > 0) {
            content = Arrays.toString(args);
        }
        logger.info("方法名：" + methodName);
        logger.info("内容：" + content);
        //记录推送日志
//        logger.info("请求地址 : " + request.getRequestURL().toString());
//        logger.info("HTTP METHOD : " + request.getMethod());
//        logger.info("IP : " + request.getRemoteAddr());
//        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
//                + joinPoint.getSignature().getName());
//        logger.info("参数 : " + Arrays.toString(joinPoint.getArgs()));

    }
//    @Around("execution(public * com.yzly.demo.controller.TCPushController.*(..))")
//    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
//        String methodName = pjp.getSignature().getName();
//        Object[] args = pjp.getArgs();
//        JSONObject data = (JSONObject) args[0];
//        logger.info("方法名：" + methodName);
//        logger.info("内容1：" + data.toString());
//        return pjp.proceed(args);
//
//    }
}
